package com.yidu.finance.view;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.Part;

import com.yidu.finance.dao.MemberDao;
import com.yidu.finance.model.Member;


/**
 *
 * 描述: 会员Servlet
 * @author liandyao
 * @date 2019年9月25日 上午9:33:06
 * @version 1.0
 */
@MultipartConfig
@WebServlet("/MemberServlet")
public class MemberServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    

	MemberDao dao = new MemberDao();
 
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String action = request.getParameter("action");
		if("showList".equals(action)){
			 
		}else if("regist".equals(action)){
			regist(request,response);
		}else if("login".equals(action)){
			login(request,response);
		}else if("exit".equals(action)){
			exit(request,response);
		}
	}
	
	/**
	 * 退出登录
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	private void exit(HttpServletRequest request, HttpServletResponse response) throws IOException {
		
		HttpSession session = request.getSession();
		session.invalidate();//清空所有的session
		
		//session.removeAttribute("member");
		
		
		
		response.sendRedirect("pages/member/login.jsp");
		
		
	}

	/**
	 * 登录
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//账号
		String account = request.getParameter("account");
		 
		//密码
		String password = request.getParameter("password");
		//验证码
		String memberCode = request.getParameter("memberCode");
		
		HttpSession session = request.getSession();
		String sid = session.getId();
		System.out.println("session地址:"+sid);
		String code = (String)session.getAttribute("code");
		
		
		/*if("".equals(memberCode)){
			request.setAttribute("msg", "验证码不能为空！");
			request.getRequestDispatcher("pages/member/login.jsp").forward(request, response);	
			return ;
		}else if(!memberCode.equalsIgnoreCase(code)){
			request.setAttribute("msg", "请填写正确的验证码！");
			request.getRequestDispatcher("pages/member/login.jsp").forward(request, response);	
			return ;
		}*/
		
		Member member = dao.selectAP(account, password);
		
		if(member==null){
			request.setAttribute("msg", "账号或密码错误！");
			request.getRequestDispatcher("pages/member/login.jsp").forward(request, response);	
		}else if(member!=null){
			//如果登录成功,就将member放入session中
			session.setAttribute("member", member);
			 
			//response.sendRedirect("FinanceServlet?action=showList");
			response.sendRedirect("pages/index.jsp");
		}
	}

	/**
	 * 注册
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void regist(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//会员名称
		String memberName = request.getParameter("memberName");
		if(memberName.equals("")){
			request.setAttribute("msg", "会员名不能为空！");
			request.getRequestDispatcher("pages/member/regist.jsp").forward(request, response);
			return ;
		}
		//账号
		String account = request.getParameter("account");
		if(account.equals("")){
			request.setAttribute("msg", "账号不能空！");
			request.getRequestDispatcher("pages/member/regist.jsp").forward(request, response);
			return;
		}
		//密码
		String password = request.getParameter("password");
		if(password.equals("")){
			request.setAttribute("msg", "密码不能为空！");
			request.getRequestDispatcher("pages/member/regist.jsp").forward(request, response);
			return ;
		}
		//照片
		Part part = request.getPart("photo");
		InputStream is = part.getInputStream();
		ServletContext context = request.getServletContext();
		String realPath = context.getRealPath("/upload");
		String fileName = part.getSubmittedFileName();
		int point = fileName.lastIndexOf(".");
		String postfix = fileName.substring(point);
		fileName = UUID.randomUUID().toString()+postfix;
		System.out.println("照片路径："+realPath+"/"+fileName);
		OutputStream os = new FileOutputStream(realPath+"/"+fileName);
		byte b []= new byte[1024];
		int len = 0;
		while((len=is.read(b))>-1){
			os.write(b, 0, len);
		}
		os.close();
		is.close();
		String filePath = "upload/"+fileName;
		//手机号
		Integer phone = Integer.valueOf(request.getParameter("phone"));
		if(phone.equals("")){
			request.setAttribute("msg", "手机号不能为空！");
			request.getRequestDispatcher("pages/member/regist.jsp").forward(request, response);
			return ;
		}
		//邮箱号
		String email = request.getParameter("email");
		//余额
		Integer balace = 0;
		//增加会员
		Member member = new Member();
		member.setMemberName(memberName);
		member.setMemberAccount(account);
		member.setMemberPassword(password);
		member.setMemberImage(filePath);
		member.setMemberPhone(phone);
		member.setMemberEmail(email);
		member.setMemberBalance(balace);
		
		int rows = dao.insert(member);
		if(rows>0){
			request.setAttribute("msg", "注册成功！");
			request.getRequestDispatcher("pages/member/login.jsp").forward(request, response);
			return ;
		}else{
			request.setAttribute("msg", "注册失败！");
			request.getRequestDispatcher("pages/member/regist.jsp").forward(request, response);
			return ;
		}
		
	}
	 
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
